<?php 
class Zend_Template_Form {
    private $_applicationName = 'Application';
    private $_form;
    private $_elements;
    
    public function __construct($form, $elements, $applicationName = 'Application'){        
        $this->_form = $form;
        $this->_elements = $elements;
        $this->_applicationName = $applicationName;
    }
    
    /**
     * Write file
     * @author tien.nguyen
     */
    public function writeFile(){
        $template = $this->render();
        
        $path = PATH_PROJECT_GENERAL.'/'.
            NAME_PROJECT_GENERAL.'/application/forms/'. 
            $this->_form->form_name.'.php';
        
        $hand = fopen($path, 'w');
        fwrite($hand, $template);
        fclose($hand);
    }

    /**
     * General form
     * @author tien.nguyen
     * @return string
     */    
    public function render(){
        /*
        $arrReplace = array(
            'application' => $this->_applicationName,
            'form_name' => $this->_form->form_name,
            'form_elements' =>  $this->getFormElements(),
            'form_extends' => $this->_form->form_extends
        );
        $template = $this->readTemplate();
        
        //replace variable in form
        foreach($arrReplace as $key => $value){
            $template = str_replace('{'.$key.'}', $value, $template);
        }
        
        //replace form 
        return $template;
        */
        global $smarty;
        $smarty->assign('form', $this->_form);
        $smarty->assign('elements', $this->_elements);
        $smarty->assign('template_path', PATH_STRUCTURE.'/zend-framework/templates');
        return $smarty->fetch(PATH_STRUCTURE.'/zend-framework/templates/form.tpl');
        
    }
    
    /**
     * Render elements
     * @author tien.nguyen
     * @return string
     */
    public function renderElement($element, $templateName){
        $templateElement = $this->readTemplateElements($templateName);
        //replace elements
        //variable elements
        $elementVariable = strtolower(substr($element->element_name, 0, 1)).
                substr($element->element_name, 1);
        $templateElement = str_replace('{element_variable}', $elementVariable, $templateElement);
         
        //add variable in database
        foreach($element as $key => $value){
            $templateElement = str_replace('{'.$key.'}', utf8_encode($value), $templateElement);  
        }  
        return $templateElement;
    }
    
    /**
     * Get form elements
     * @author tien.nguyen
     */
    public function getFormElements(){
       $templateElements = "";
       foreach($this->_elements as $element){
            $templateElements .= $this->renderElement($element, $element->element_type_template);
       } 
       return $templateElements;
    }
    
    /**
     * read template 
     * @param $templateName
     * @return string
     */
    public function readTemplateElements($templateName){
        $template = file_get_contents(PATH_STRUCTURE.'/zend-framework/templates/elements/'.$templateName.'.tpl');
       return $template; 
    }
    
    /**
     * Reade template
     * @return string
     * @author tien.nguyen
     */
    public function readTemplate(){
        $template = file_get_contents(PATH_STRUCTURE.'/zend-framework/templates/form.tpl');
       return $template; 
    }
}